/**
 * 租户套餐菜单
 * @author luguoxiang
 * @date 2025-05-05
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
import {
  BaseEntity,
  IConfigSchema,
  LgBizSelect,
  LgBizSelectProps,
  LgBizTree,
  LgBizTreeProps,
  LgDictProps,
  LgIcon,
  LgText,
  LgTextProps,
} from '@/soar';
import api from './api';
import componentApi from '@/pages/system/auth/component/api';
import IconPicker from '@/components/IconPicker/index.vue';

export interface TenantComboMealMenu extends BaseEntity {
  /** 编码（路由名称，权限识别） */
  code?: any;
  /** 套餐id */
  comboMealId?: any;
  /** 组件 */
  componentId?: number;
  /** 创建人 */
  createBy?: any;
  /** 创建时间 */
  createTime?: any;
  /** 是否隐藏子菜单（0否，1是） */
  hideChildren?: any;
  /** 图标 */
  icon?: any;
  /**  */
  id?: any;
  /** 是否缓存 */
  keepalive?: any;
  /** 父id(0表示最顶级) */
  parentId?: any;
  /** 路径 */
  path?: any;
  /** 重定向路径 */
  redirect?: any;
  /** 是否共享菜单 */
  shared?: any;
  /** 排序 */
  sort?: any;
  /** 状态：0正常，1隐藏菜单，2禁用菜单 */
  status?: any;
  /** 名称(标题) */
  title?: any;
  /** 类型(0按钮，1菜单，2目录，3系统) */
  type?: any;
  /** 更新人 */
  updateBy?: any;
  /** 更新时间 */
  updateTime?: any;
}
const TYPES = {
  0: [3, 2, 1],
  1: [3, 2],
  2: [3, 2],
  3: [3],
};

const fieldList: IConfigSchema<TenantComboMealMenu>[] = [
  {
    field: 'type',
    label: '类型',
    dataType: 'dict',
    params: 'MenuTypeEnum',
    table: {
      width: 80,
      sorter: true,
    },
    search: {
      componentProps: {
        hiddenValue: 0,
      } as LgDictProps,
    },
    form: {
      rules: true,
      componentProps: {
        component: 't-radio-group',
        hiddenValue: 0,
      } as LgDictProps,
    },
  },
  {
    field: 'parentId',
    label: '所属父级',
    dataType: 'string',
    table: false,
    search: false,
    form: {
      rules: true,
      vif({ data }): boolean {
        return data.type !== 3;
      },
      component: LgBizTree,
      componentProps({ data }) {
        return {
          fields: 'id,title,parentId',
          filters: [`type-in~${TYPES[data.type]?.join('~') ?? 3}`],
          data: api.query,
          prepend: { value: '0', label: '根节点' },
        } as LgBizTreeProps;
      },
    },
    detail: {
      valueProp: 'modelValue',
      component: LgText,
      componentProps: {
        api: api.query,
        labelField: 'title',
      } as LgTextProps,
    },
  },
  {
    field: 'title',
    label: '名称',
    dataType: 'string',
    form: {
      rules: true,
    },
    table: {
      order: -1,
    },
  },
  {
    field: 'icon',
    label: '图标',
    dataType: 'string',
    search: false,
    table: {
      width: 70,
      align: 'center',
      cell: (h, { row }) => h(LgIcon, { value: row.icon }),
    },
    detail: {
      component: 't-icon',
      valueProp: 'name',
      vif({ data }): boolean {
        return data.type !== 0;
      },
    },
    form: {
      vif({ data }): boolean {
        return data.type !== 0;
      },
      component: IconPicker,
    },
  },
  {
    field: 'path',
    label: '路径',
    dataType: 'string',
    form: {
      rules: true,
      vif({ data }) {
        return data.type !== 0;
      },
      slots: 'path',
    },
    table: {
      ellipsis: true,
    },
    detail: {
      vif({ data }) {
        return data.type !== 0;
      },
    },
  },
  {
    field: 'redirect',
    label: '重定向路径',
    dataType: 'string',
    form: {
      vif({ data }) {
        return data.type === 2 || data.type === 3;
      },
    },
    table: {
      ellipsis: true,
    },
    detail: {
      vif({ data }) {
        return data.type === 2 || data.type === 3;
      },
    },
  },
  {
    field: 'sort',
    label: '排序',
    dataType: 'number',
    table: {
      width: 120,
      slots: 'sort',
      sorter: true,
    },
    form: {
      vif({ data }): boolean {
        return data.type !== 0;
      },
    },
    detail: {
      vif({ data }): boolean {
        return data.type !== 0;
      },
    },
  },
  {
    field: 'status',
    label: '状态',
    dataType: 'dict',
    params: 'MenuStatusEnum',
    form: {
      rules: true,
      componentProps: {
        component: 't-radio-group',
      } as LgDictProps,
    },
    table: {
      width: 80,
    },
  },
  {
    field: 'hideChildren',
    label: '隐藏子菜单',
    dataType: 'boolean',
    form: {
      vif({ data }) {
        return data.type === 2;
      },
    },
    detail: {
      vif({ data }) {
        return data.type === 2;
      },
    },
    table: false,
  },
  {
    field: 'componentId',
    label: '组件',
    dataType: 'number',
    table: {
      colKey: 'componentName',
      dataType: 'string',
    },
    form: {
      component: LgBizSelect,
      componentProps: {
        data: componentApi.query,
        fields: 'id,name,path',
        search: true,
        labelField: '【${name}】${path}',
      } as LgBizSelectProps,
    },
  },
];

export default fieldList;
